﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 网站轮播图仓储
/// </summary>
public class MdBannerRep : BaseRep<MdBannerModel>
{
    private static string sqlFields = string.Format(@"
        `banner_id`,
        `title`,
        `picture`,
        `url`,
        `category`,
        `sort`,
        `status`,
        `del_flag`,
        `create_by`,
        `create_time`,
        `update_by`,
        `update_time`
    ");

    /// <summary>
    /// 通过编号获取轮播图信息
    /// </summary>
    /// <param name="id">轮播图编号</param>
    /// <returns>MdProductModel</returns>
    public MdBannerModel? GetMdBannerById(long id)
    {
        string sqlWhere = string.Format(@" and `banner_id` = @BannerId ");

        return QuerySingle(sqlFields, sqlWhere, new { BannerId = id });
    }

    /// <summary>
    /// 获取网站轮播图列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒叙</param>
    /// <returns>IEnumerable<MdBannerModel></returns>
    public IEnumerable<MdBannerModel> GetMdBannerList(MdBannerModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Title))
        {
            sqlWhere += " and `title` like concat('%', @Title, '%') ";
        }
        if (model.Category > 0)
        {
            sqlWhere += " and `category` = @Category ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }
        sqlWhere += " and del_flag = '0' ";
        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取网站轮播图列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<MdBannerModel></returns>
    public Page<MdBannerModel> GetMdBannerPageList(MdBannerModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Title))
        {
            sqlWhere += " and `title` like concat('%', @Title, '%') ";
        }
        if (model.Category > 0)
        {
            sqlWhere += " and `category` = @Category ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }
        sqlWhere += " and del_flag = '0' ";
        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过编号更新网站轮播图
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>long</returns>
    public long UpdateMdBannerById(MdBannerModel model)
    {
        var sqlSet = string.Empty;

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.Title))
        {
            sqlSet += " `title` = @Title, ";
        }

        if(!string.IsNullOrWhiteSpace(model.Picture))
        {
            sqlSet += " `picture` = @Picture, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Url))
        {
            sqlSet += " `url` = @Url, ";
        }

        if (model.Category > 0)
        {
            sqlSet += " `category` = @Category, ";
        }

        if (model.Sort > -1)
        {
            sqlSet += " `sort` = @Sort, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `del_flag` = '0' and `banner_id` = @BannerId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过编号删除网站轮播图
    /// </summary>
    /// <param name="ids">编号多个逗号分隔</param>
    /// <returns>int</returns>
    public int DeleteMdBannerById(string ids)
    {
        string sqlWhere = " and `banner_id` in @BannerIds ";

        return Delete(sqlWhere, new { BannerIds = ids.Split(',').ToArray() });
    }
}

